import axios from '@/js/ajax';
import { setToken, getToken } from '@/utils/token-util';
import store from '@/store';

// 登录逻辑
export async function login(data) {
  const res = await axios.post('api/v1/manage/user/login', data);
  if (res.data.code == 0 || res.data.code == 200) {
    setToken(res.data.data.token);
    return res;
  }
  return Promise.reject(new Error('出错了', res));
}

// 用户信息
export async function getUserInfo() {
  if (getToken()) {
    const res = await axios.get('api/v1/manage/sys/userInfo');
    console.log(res);
    if (res.data.code == 200 || res.data.code == 0) {
      store.dispatch('user/fetchUserInfo', res);
      return res;
    }
    return Promise.reject(new Error(res));
  }
}
